package com.zhiyou100.video.webaction.admin;

import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhiyou100.video.model.Admin;
import com.zhiyou100.video.service.AdminService;
import com.zhiyou100.video.utils.MD5Utils;

@SuppressWarnings("all")
@Controller("AdminAction")
@Scope(scopeName="prototype")
public class AdminAction extends ActionSupport implements ModelDriven<Admin> {
	private Admin ad = new Admin();
	@Override
	public Admin getModel() {
		return ad;
	}
	@Autowired
	AdminService as;
	public String login() throws Exception {
		return SUCCESS;
	}

	public String adlogin() throws Exception {
		ad.setLoginPwd(MD5Utils.getMD5(ad.getLoginPwd()));
		DetachedCriteria dc = DetachedCriteria.forClass(Admin.class);
		dc.add(Restrictions.eq("loginName", ad.getLoginName())).add(Restrictions.eq("loginPwd", ad.getLoginPwd()));
		List<Admin> list=as.selectlogin(dc);
		if(list.isEmpty()){
			ActionContext.getContext().put("error", "密码或用户名错误");
			return "login";
		}
		 ActionContext.getContext().getSession().put("admin", list.get(0));
		return "videolist";
	}
	public String loginout()throws Exception {
		
		ActionContext.getContext().getSession().remove("admin");
		
		return "login";
		
	}
}
